" This is my vim configuration file 
" Do a little bit more every day
" Maintainer   : zhujiadong
" Create time  : 2016-12-27 Tue 17:05:20
" Last modified: 2017-01-20 五 15:04:57


"""""""""""""""""""""""""""""""""""""""""""""""""
" ==> normal settins
"""""""""""""""""""""""""""""""""""""""""""""""""

set number                      
set hlsearch                     
set incsearch
set cursorline                   "突出显示当前行
set backspace=indent,eol,start   "升级之后不能删除到上一行了,需要设置backspace的行为
set shortmess=atI                "启动vim时不显示帮助乌干达儿童的提示
set expandtab                    "是否在缩进和遇到 Tab 键时使用空格替代
set tabstop=4                    "tab的宽度,默认是8
set shiftwidth=4                 "缩进的空格数
set softtabstop=4                "软制表符宽度,设置为非零数值后使用Tab键和Backspace时光标移动的格数等于该数值,但实际插入的字符仍受tabstop和expandtab控制
set autoindent 
set smartindent 
"set cindent 
autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class 


"""""""""""""""""""""""""""""""""""""""""""""""""
" ==> vundle vim插件管理工具
"""""""""""""""""""""""""""""""""""""""""""""""""

"git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
set nocompatible    
filetype off         "required
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
"call vundle#begin('path')  "where vundle should install plugins
"let Vundle Manage itself
Plugin 'VundleVim/Vundle.vim'
"other plugins
Plugin 'scrooloose/nerdtree'
Plugin 'vim-scripts/taglist.vim'
Plugin 'Valloric/YouCompleteMe'
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
Plugin 'flazz/vim-colorschemes'
Plugin 'felixhummel/setcolors.vim'
Plugin 'godlygeek/tabular'
Plugin 'plasticboy/vim-markdown'
Plugin 'suan/vim-instant-markdown'
Plugin 'jlanzarotta/bufexplorer' 
Plugin 'vim-scripts/winmanager'
Plugin 'Raimondi/delimitMate'
Plugin 'Yggdroot/indentLine'
Plugin 'tpope/vim-commentary'
Plugin 'SirVer/ultisnips'
Plugin 'honza/vim-snippets'
call vundle#end()
filetype plugin indent on

colorscheme CandyPaper

"""""""""""""""""""""""""""""""""""""""""""""""""
" ==> 插件管理pathogen,这里不再使用
"""""""""""""""""""""""""""""""""""""""""""""""""

"pathogen.vim 
"Manage your 'runtimepath' with ease. In practical terms, pathogen.vim makes it super easy to install plugins and runtime files in their own private directories
"Installation
"Install to ~/.vim/autoload/pathogen.vim
"mkdir -p ~/.vim/autoload ~/.vim/bundle && \
"curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
"add this to your vimrc: 
"execute pathogen#infect()
"filetype plugin indent on
"Now any plugins you wish to install can be extracted to a subdirectory under ~/.vim/bundle, and they will be added to the 'runtimepath'
"以上是配置path,如果不配置的话安装插件将出现问题,以后安装插件只需把插件放到.vim/bundle/下在做一些简单的配置即可
"生成插件的帮助文档,:helptags ~/.vim/doc/* example:  :help NERD_tree.txt

"execute pathogen#infect()
"filetype plugin indent on  "允许使用插件和缩进检测

 
"""""""""""""""""""""""""""""""""""""""""""""""""
" ==> NERDTree
"""""""""""""""""""""""""""""""""""""""""""""""""

"open a NERDTree automatically when vim starts up  
"autocmd vimenter * NERDTree
autocmd StdinReadPre * let s:std_in=1
"open a NERDTree automatically when vim starts up if no files were specified
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif
"open NERDTree automatically when vim starts up on opening a directory
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists("s:std_in") | exe 'NERDTree' argv()[0] | wincmd p | ene | endif
"如果不配置的话安装插件将出现问题关闭最后一个文件那么同时关闭NERDTree
"autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") &&b:NERDTreeType == "primary") | q | endif
"map a specific key or shortcut to open NERDTree  快捷键ctrl+n 打开或关闭NERDTree
map <C-n> :NERDTreeToggle<CR>
"改变箭头的形状,使用默认的就可以了,可能有些时候显示不出来,找到NERD_tree.vim第71行可以修改
"let g:NERDTreeDirArrowExpandable='>'
"let g:NERDTreeDirArrowCollapsible='-'
"let loaded_nerd_tree=1            "关闭NERDTree
let NERDTreeCaseSensitiveSort=0  "目录排序是否和大小写相关,默认不相关 value = 0 or 1
let NERDTreeChDirMode=2  "是否更换工作目录,0永不更换,1NERDTree第一次初始化时的目录,之后不会随着NERDTree变化 2随着NERDTree的变化而变化  
"let NERDTreeHijackNetrw=1 不明白
let NERDTreeIgnore=['\.pyc$']      "文件过滤,比如'\.py$'会忽略所有python源文件  '.d$[[dir]]' '.o$[[file]]' 忽略.d结尾的目录和.o结尾的文件 []表示不忽略任何文件  f动态开启或关闭 
"let NERDTreeBookmarksFile=~/.NERDTreeBookmarks "在哪里保存bookmarks文件,默认是$HOME/.NERDTreeBookmarks
let NERDTreeBookmarksSort=1        "是否对bookmarks list排序
let NERDTreeMouseMode=1            "鼠标点击模式 1 2 3 单击或双击  
"let NERDTreeQuitOnOpen=1          "打开一个文件时关闭NERDTree
"let NERDTreeShowBookmarks=0       "是否显示bookmarks list   B可以打开或关闭
let NERDTreeShowFiles=1            "显示普通文件,0只显示目录     F
let NERDTreeShowHidden=0           "是否显示隐藏文件             I
let NERDTreeShowLineNumbers=0      "是否显示行号
let NERDTreeSortOrder=['\/$','*']  "文件出现的顺序  
"['*','\/$']  目录最后显示
"[]           Everything will simply appear in alphabetical order
"['\/$', '\.rb$', '\.php$', '*', '\.swp$',  '\.bak$', '\~$'] 
"let NERDTreeWinPos="right"
let NERDTreeWinSize=24
let NERDTreeMinimalUI=1            "不显示'Press ? for help' 
"let NERDTreeCascadeSingleChildDir=1 不明白
let NERDTreeAutoDeleteBuffer=1     "删除文件时不再询问是否删除buffer(未保存的内容)


"""""""""""""""""""""""""""""""""""""""""""""""""
" ==> taglist
"""""""""""""""""""""""""""""""""""""""""""""""""

let Tlist_Auto_Open=0                      "启动vim时自动打开taglist
let Tlist_Auto_Update=1                    "编辑新文件时taglist自动更新
"let Tlist_Ctags_Cmd='/usr/local/bin/ctags'"如果PATH没有保存catgs的路径,那么手动指向ctags的路径
let Tlist_Show_One_File=1                  "不同时显示多个文件的tag，只显示当前文件的
let Tlist_Exit_OnlyWindow=1                "如果taglist窗口是最后一个窗口，则退出vim
let Tlist_Close_On_Select=0                "当选中一个tag时关闭taglist
let Tlist_Use_Right_Window=1               "在右侧窗口中显示taglist窗口
"nnoremap <F8> :TlistToggle<CR>    
map <silent> <C-l> :TlistToggle<CR>    
let Tlist_Use_SingleClick=1                "鼠标单击跳转到一个tag
let Tlist_Process_File_Always=1            "关闭taglist窗口时仍处理文件
let Tlist_Sort_Type="order"                "taglist默认是按出现的顺序排序的,也可以使用名称排序\"name\",按s可以切换
let Tlist_Compact_Format=1                 "taglist窗口压缩,去除空行
let Tlist_WinWidth=45                      "taglist窗口宽度
let Tlist_Use_SingleClick=1                "鼠标单击进入一个tag,鼠标调整窗口大小
let Tlist_Display_Prototype=1              "显示原型而不是tag name
let Tlist_Display_Tag_Scope=1              "不显示tag names所属的类型 不起作用...
let Tlist_Enable_Fold_Column=1             "允许折叠
let Tlist_GainFocus_On_ToggleOpen=1        "打开taglist时 控制光标所在位置

"taglist自定义设置
"<language_name>;flag1:name1;flag2:name2;flag3:name3
"ctags --list-maps=all                     "列出所有语言的名称和对应的后缀
"ctags --list-kinds=all                    "列出所有语言和其对应的flag and default set of taglist 
"let tlist_cpp_settings='c++;c:class;f:function'  
"对于c++文件只显示类和函数  cpp is the vim filetype :set filetype 
let tlist_python_settings='python;c:classes:;f:functions:;m:class members:;v:variables:;i:imports:'

"""""""""""""""""""""""""""""""""""""""""""""""""
" ==> YouCompleteMe ycm
"""""""""""""""""""""""""""""""""""""""""""""""""

let g:ycm_python_binary_path='python'               "到path中找到python运行jedi,如果是一个绝对路径,那么就使用该python解释器
let g:ycm_global_ycm_extra_conf='/home/melo/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py' "for c family langs 
let g:ycm_confirm_extra_conf=0                      "打开vim时不再询问是否加载ycm_extra_conf.py配置
"nnoremap <F5> :YcmForceCompileAndDiagnostics<CR>   "<F5>键,强制YCM编译文件并检查错误
let g:ycm_min_num_of_chars_for_completion=3         "至少输入几个字符开始补全,如果设置的值很大,相当于关掉identifier completion engine,只剩下semantic engine(. -> ::补全)
let g:ycm_min_num_identifier_candidate_chars=0      "控制候选菜单中最小的字符长度 0=没限制,显示所有候选者,该选项对semantic engine没有影响
let g:ycm_auto_trigger=1                            "自动触发补全,0表示关闭补全,包括semantic补全
let g:ycm_filetype_whitelist={'*':1}                "白名单,控制对于哪些文件需要打开ycm,YCM will work only in filetypes that both the whitelist and the blacklist allow
"filetype exists in this dict will not use YCM
let g:ycm_filetype_blacklist={'tagbar':1, 'qf':1, 'notes':1,  'markdown':1, 'unite':1, 'text':1, 'vimwiki':1, 'pandoc':1, 'infolog':1, 'mail':1}
let g:ycm_filetype_specific_completion_to_disable={'gitcommit':1} "指定那些文件关闭semantic complete engine
let g:ycm_show_diagnostics_ui=1                     "only for C family langs
"let g:ycm_error_symbol='>>'
"let g:ycm_warning_symbol='>>'
let g:ycm_complete_in_comments=1                    "允许注释补全
let g:ycm_complete_in_strings=1                     "允许字符串补全
let g:ycm_collect_identifiers_from_comments_and_strings=0
let g:ycm_collect_identifiers_from_tags_files=0
let g:ycm_seed_identifiers_with_syntax=1            "语法关键字补全
let g:ycm_add_preview_to_completeopt=1              "选择函数时是否打开函数说明的窗口
let g:ycm_autoclose_preview_window_after_completion=1
let g:ycm_key_list_select_completion=['<Down>','<Enter>']
let g:ycm_key_list_previous_completion=['<S-TAB>', '<Up>']
"let g:ycm_disable_for_files_larger_than_kb=1000     "超过1000kb的文件不做补全
inoremap <expr> <CR>  pumvisible() ? "\<C-y>" : "\<CR>"   
"set completeopt=longest,menu    "让Vim的补全菜单行为与一般IDE一致(参考VimTip1228)


"""""""""""""""""""""""""""""""""""""""""""""""""
" ==> airline
"""""""""""""""""""""""""""""""""""""""""""""""""

set laststatus=2                                                "0 never 1 only u have more than 2 windows,the status bar show 2 always show
let g:airline_inactive_collapse=1                               "允许不活跃的窗口只显示文件名
let g:airline_section_b='%{strftime("%Y-%m-%d %a %H:%M:%S")}'   "设置第二段显示的内容
"let g:airline_section_c='%{getcwd()}'
let g:airline_section_y='BN: %{bufnr("%")}'                     "设置右边第二段显示buffer的编号
let g:airline#extensions#tabline#enabled=1                      "显示tabline
let g:airline#extensions#tabline#buffer_nr_show=1               "显示buffer的编号
let g:airline#extensions#tabline#left_sep=' '                   "tabline buffer的分隔符
let g:airline#extensions#tabline#left_alt_sep='|'    
let g:airline#extensions#whitespace#enabled=0                   "不显示空白符号计数
let g:airline#extensions#whitespace#symbol='!'                  "空白符号显示符号
"let g:airline#extensions#default#section_truncate_width={'b': 79,'x': 60,'y': 88,'z': 45,'warning': 80,'error': 80,}
let g:airline#extensions#default#section_truncate_width={}      "disable truncate
let g:airline_powerline_fonts=1
if !exists('g:airline_symbols')
    let g:airline_symbols = {}
endif
let g:airline_left_sep = '⮀'
let g:airline_left_alt_sep = '⮁'
let g:airline_right_sep = '⮂'
let g:airline_right_alt_sep = '⮃'
let g:airline_symbols.branch = '⭠'
let g:airline_symbols.readonly = '⭤'
let g:airline_symbols.linenr = '⭡'
let g:airline_detect_modified=1
"设置切换Buffer快捷键"
nnoremap <C-b> :bn<CR>
let g:airline_theme='vice'    
let g:airline#extensions#default#layout=[ [ 'a', 'b', 'c' ,'error','warning'], [ 'x', 'y', 'z' ] ]

"------------------airline-ycm--------------------------
let g:airline#extensions#ycm#enabled=1
let g:airline#extensions#ycm#error_symbol='errs:'
let g:airline#extensions#ycm#warning_symbol='warnngs:'

function! AccentDemo()
    let keys = ['a','b','c','d','e','f','g','h']
    for k in keys
        call airline#parts#define_text(k, k)
    endfor
    call airline#parts#define_accent('a', 'red')
    call airline#parts#define_accent('b', 'green')
    call airline#parts#define_accent('c', 'blue')
    call airline#parts#define_accent('d', 'yellow')
    call airline#parts#define_accent('e', 'orange')
    call airline#parts#define_accent('f', 'purple')
    call airline#parts#define_accent('g', 'bold')
    call airline#parts#define_accent('h', 'italic')
    let g:airline_section_a = airline#section#create(keys)
endfunction
"autocmd VimEnter * call AccentDemo()


"""""""""""""""""""""""""""""""""""""""""""""""""
" ==> markdown syntax highlight plugin 
"""""""""""""""""""""""""""""""""""""""""""""""""

let g:vim_markdown_folding_disabled=0                           "不使用折叠
let g:vim_markdown_folding_style_pythonic=0                     "使用python折叠样式
let g:vim_markdown_folding_level=3                              "设置标题折叠级别1-6
let g:markdown_fenced_languages=['html','python','bash=sh']     "代码块语法高亮显示


"""""""""""""""""""""""""""""""""""""""""""""""""
" ==> vim-instant-markdown  
"""""""""""""""""""""""""""""""""""""""""""""""""

"sudo apt-get install npm 
"sudo npm -g install instant-markdown-d
"sudo apt-get install nodejs-legacy  --> debain
"需要有浏览器,curl 
let g:instant_markdown_slow=0                              "是否实时更新,0达到一定的条件在更新
let g:instant_markdown_autostart=1                         "是否自动开启,:InstantMarkdownPreview手动开启
"let g:instant_markdown_open_to_the_world=1                "是否允许外部浏览器访问,不显示内容... strange
let g:instant_markdown_allow_unsafe_content=1              
autocmd BufNewFile,BufReadPost *.md set filetype=markdown  "低版本的vim可能只识别.markdown或.mdown,这里强制处理<anything>.md


"""""""""""""""""""""""""""""""""""""""""""""""""
" ==> bufexplorer
"""""""""""""""""""""""""""""""""""""""""""""""""

let g:bufExplorerDefaultHelp=0
let g:bufExplorerShowRelativePath=1


"""""""""""""""""""""""""""""""""""""""""""""""""
" ==> winmanager 
"""""""""""""""""""""""""""""""""""""""""""""""""

"左侧显示nerdtree和bufexplorer
autocmd vimenter * if !argc() | NERDTree | endif
let g:NERDTree_title="[NERDTree]"  
let g:winManagerWindowLayout="NERDTree|BufExplorer"
function! NERDTree_Start()  
    exec 'NERDTree'  
endfunction  
function! NERDTree_IsValid() 
    return 1 
endfunction
"winmanager.vim line 1065 add exec 'q'
nnoremap <silent> <c-F11> :WMToggle<CR>


"""""""""""""""""""""""""""""""""""""""""""""""""
" ==> commentary
"""""""""""""""""""""""""""""""""""""""""""""""""

"Visual模式下gc命令可以注释选中的行
"普通模式下gcc指令可以快速注释一行
"gcu可以撤销注释
autocmd FileType python,shell set commentstring=#\ %s                 "设置Python注释字符

"""""""""""""""""""""""""""""""""""""""""""""""""
" ==> indentline
"""""""""""""""""""""""""""""""""""""""""""""""""

let g:indentLine_enabled=1                                            "whether use this plugin
let g:indentLine_char='┊'                                             "¦ | ¦ ┆ │
let g:indentLine_setColors=1                                          "whether highlight conceal
let g:indentLine_showFirstIndentLevel=1                               "show first level
let g:indentLine_first_char='┊'                                       "first level char 
let g:indentLine_fileType=['c','cpp','python','java','xml']
let g:indentLine_leadingSpaceEnabled=0                                "whether show leader spacechar
let g:indentLine_leadingSpaceChar='·' 

"""""""""""""""""""""""""""""""""""""""""""""""""
" auto generate code when edit a specific file
"""""""""""""""""""""""""""""""""""""""""""""""""

autocmd BufNewFile *.py,*.c,*.sh exec ":call SetTitle()"
func SetTitle()
    if &filetype == "sh" 
        call setline(1,"\#!/bin/bash")
        call setline(2,"\#File Name    : ".expand("%"))
        call setline(3,"\#Author       : zhujiadong")
        call setline(4,"\#Mail         : melo <826695779@qq.com>")
        call setline(5,"\#Create time  : ".strftime("%Y-%m-%d %a %H:%M:%S"))
        call setline(6,"\#Last modified: <date time>")
    endif                                                                
    if &filetype == "python"
        call setline(1,"\#!/usr/bin/env python")
        call setline(2,"\# -*- coding: utf-8 -*-")
        call setline(3,"\#File Name    : ".expand("%"))
        call setline(4,"\#Author       : zhujiadong")
        call setline(5,"\#Mail         : melo <826695779@qq.com>")
        call setline(6,"\#Create time  : ".strftime("%Y-%m-%d %a %H:%M:%S"))
        call setline(7,"\#Last modified: <date time>")
    endif  
    if &filetype == 'c'
        call setline(1,"\/***********************************************")
        call setline(2,"\ *Target       : TODO ")
        call setline(3,"\ *File Name    : ".expand("%"))
        call setline(4,"\ *Author       : zhujiadong")
        call setline(5,"\ *Mail         : melo <826695779@qq.com>")
        call setline(6,"\ *Create time  : ".strftime("%Y-%m-%d %a %H:%M:%S"))
        call setline(7,"\ *Last modified: <date time>")
        call setline(8,"\ **********************************************/")
        call setline(9,"\ ")
        call setline(10,"\#include <stdio.h>")
        call setline(11,"\int main(int argc,char **argv)")
        call setline(12,"\{")
        call setline(13,"\     ")
        call setline(14,"\    return 0;")
        call setline(15,"\}")
    endif  
endfunc
autocmd BufNewFile * normal G o

"退出文件时,修改时间 
autocmd BufWritePre,FileWritePre *.c,*.py,.vimrc,*.md,*.sh ks|call LastModified()|'s
func LastModified()
	let l = line("$")
	" * # " // 
	exe "1," . l . 'g/^\s*\(\*\|#\|"\|\/\/\)\?\s*[L]ast modified:\s/s/^\(\s*\(\*\|#\|"\|\/\/\)\?\s*[L]ast modified:\s\).*/\1' .
	\ strftime("%Y-%m-%d %a %H:%M:%S")
endfunc

map <F5> :call CompileRunGcc()<CR>
func! CompileRunGcc()
    exec "w"
    if &filetype == 'c'
        exec "!g++ % -o %<"
        exec "! ./%<"
    elseif &filetype == 'cpp'
        exec "!g++ % -o %<"
        exec "! ./%<"
    elseif &filetype == 'java' 
        exec "!javac %" 
        exec "!java %<"
    elseif &filetype == 'sh'
        exec "!chmod a+x %"
        :!./%
    elseif &filetype == 'python'
        exec "!python %"
    endif
endfunc
map <F6> :call Rungdb()<CR>
func! Rungdb()
    exec "w"
    exec "!g++ % -g -o %<"
    exec "!gdb ./%<"
endfunc
